import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const myOrder = (resolve) => {
  import('views/myOrder').then((module) => {
    resolve(module)
  })
}

const chooseAddress = (resolve) => {
  import('views/chooseAddress').then((module) => {
    resolve(module);
  })
}

const addAddress = (resolve) => {
  import('views/addAddress').then((module) => {
    resolve(module);
  })
}

const goodsList = (resolve) => {
  import('views/goodsList').then((module) => {
    resolve(module)
  })
}

const viewRouter = new Router({
  routes: [
    {
      path: '/product_detail2',
      component: ()=> import('views/product_detail2.vue'),
      meta: {
        title: '商品详情'
      }
    },
    {
      path: '/product_detail',
      component: ()=> import('views/product_detail.vue'),
      meta: {
        title: '商品详情'
      }
    },
    {
      path: '/',
      component: null,
      meta: {
        title: 'cvn商城'
      }
    },
    {
      path: '/goodsList',
      component: goodsList,
      meta: {
        title: '商城'
      }
    },
    {
      path: '/chooseAddress',
      component: chooseAddress,
      meta: {
        title: '选择收货地址'
      }
    },
    {
      path: '/addAddress',
      component: addAddress,
      meta: {
        title: '添加收货地址'
      }
    },
    {
      path: '/myOrder',
      component: myOrder,
      meta: {
        title: '我的订单'
      }
    }
  ]
})

viewRouter.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  let uid = to.query.uid || localStorage.getItem('uid')
  let token = to.query.token || localStorage.getItem('token')
  next()
  setTimeout(()=>{
    localStorage.setItem('uid', uid)
    localStorage.setItem('token', token)
  },40)
})


export default viewRouter
